import { defineStore } from 'pinia';
import { Local } from '/@/utils/storage';

// 站点配置状态接口
export interface ISiteConfigState {
  siteConfig: {
    /** 站点名称 */
    siteName: string;
    /** 站点Logo */
    logoId: string | null;
    /** 是否自定义部门编码 */
    customGroupCode: boolean;
    /** 是否自定义岗位编码 */
    customJobPositionCode: boolean;
    /** 短信登录 */
    smsLogin: boolean;
    /** 微信登录 */
    wechatLogin: boolean;
    /** 短信应用ID */
    smsAppId: string | null;
    /** 租户代码 */
    tenantCode: string | null;
  };
}

/**
 * 全局站点配置参数管理
 * 用于存储和管理从站点设置页面配置的参数
 */
export const useSiteConfig = defineStore('siteConfig', {
  state: (): ISiteConfigState => ({
    siteConfig: {
      siteName: '',
      logoId: null,
      customGroupCode: false,
      customJobPositionCode: false,
      smsLogin: false,
      wechatLogin: false,
      smsAppId: null,
      tenantCode: null,
    },
  }),

  getters: {
    /** 获取站点名称 */
    getSiteName: (state) => state.siteConfig.siteName,
    /** 获取Logo ID */
    getLogoId: (state) => state.siteConfig.logoId,
    /** 是否启用短信登录 */
    isSmsLoginEnabled: (state) => state.siteConfig.smsLogin,
    /** 是否启用微信登录 */
    isWechatLoginEnabled: (state) => state.siteConfig.wechatLogin,
    /** 是否自定义部门编码 */
    isCustomGroupCode: (state) => state.siteConfig.customGroupCode,
    /** 是否自定义岗位编码 */
    isCustomJobPositionCode: (state) => state.siteConfig.customJobPositionCode,
    /** 获取短信应用ID */
    getSmsAppId: (state) => state.siteConfig.smsAppId,
    /** 获取租户代码 */
    getTenantCode: (state) => state.siteConfig.tenantCode,
  },

  actions: {
    /**
     * 设置站点配置
     * @param config 站点配置对象
     */
    setSiteConfig(config: Partial<ISiteConfigState['siteConfig']>) {
      this.siteConfig = { ...this.siteConfig, ...config };
      // 同步保存到本地缓存
      this.saveSiteConfigToLocal();
    },

    /**
     * 从API数据更新站点配置
     * @param apiData 从API获取的站点配置数据
     */
    updateSiteConfigFromApi(apiData: any) {
      const config = {
        siteName: apiData.siteName || apiData.Name || '',
        logoId: apiData.logoId || apiData.LogoId || null,
        customGroupCode: apiData.customGroupCode || apiData.CustomGroupCode || false,
        customJobPositionCode: apiData.customJobPositionCode || apiData.CustomJobPositionCode || false,
        smsLogin: apiData.smsLogin || apiData.SmsLogin || false,
        wechatLogin: apiData.wechatLogin || apiData.WechatLogin || false,
        smsAppId: apiData.smsAppId || apiData.SmsAppId || null,
        tenantCode: apiData.tenantCode || apiData.TenantCode || null,
      };
      this.setSiteConfig(config);
    },

    /**
     * 保存站点配置到本地缓存
     */
    saveSiteConfigToLocal() {
      Local.set('siteConfig', this.siteConfig);
    },

    /**
     * 从本地缓存加载站点配置
     */
    loadSiteConfigFromLocal() {
      const localConfig = Local.get('siteConfig');
      if (localConfig) {
        this.siteConfig = { ...this.siteConfig, ...localConfig };
      }
    },

    /**
     * 清除站点配置
     */
    clearSiteConfig() {
      this.siteConfig = {
        siteName: '',
        logoId: null,
        customGroupCode: false,
        customJobPositionCode: false,
        smsLogin: false,
        wechatLogin: false,
        smsAppId: null,
        tenantCode: null,
      };
      Local.remove('siteConfig');
    },

    /**
     * 初始化站点配置
     * 优先从本地缓存加载，如果没有则使用默认值
     */
    initSiteConfig() {
      this.loadSiteConfigFromLocal();
    },
  },
}); 